function checkPassword(pass) {
	var req = new XMLHttpRequest();
	req.onreadystatechange=function() {
		if (req.readyState == 4 && req.status == 200) {
		    	document.getElementById("hint").innerHTML = req.responseText;
		}
	}
	var params = "pass=" + pass;
	req.open("POST","checkPasswordAjax.php", true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-length", params.length);
	req.setRequestHeader("Connection", "close");		
	req.send(params);
	return pass;
}

function validatePass(passRep) {
	var req = new XMLHttpRequest();
	req.onreadystatechange=function() {
		if (req.readyState == 4 && req.status == 200) {
		    	document.getElementById("validatePass").innerHTML = req.responseText;
		}
	}
	var params = "pass=" + document.getElementById('password').value + "&passRep=" + passRep;
	req.open("POST","validatePassAjax.php", true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-length", params.length);
	req.setRequestHeader("Connection", "close");		
	req.send(params);
}

function checkUsername(username) {
	var req = new XMLHttpRequest();
	req.onreadystatechange=function() {
		if (req.readyState == 4 && req.status == 200) {
		    	document.getElementById("usernameCheck").innerHTML = req.responseText;
		}
	}
	var params = "username=" + username;
	req.open("POST","checkUserAjax.php", true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-length", params.length);
	req.setRequestHeader("Connection", "close");		
	req.send(params);
	return pass;
}

function validateEmail(emailRep) {
	var req = new XMLHttpRequest();
	req.onreadystatechange=function() {
		if (req.readyState == 4 && req.status == 200) {
		    	document.getElementById("validateEmail").innerHTML = req.responseText;
		}
	}
	var params = "email=" + document.getElementById('email').value + "&emailRep=" + emailRep;
	req.open("POST","validateEmailAjax.php", true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-length", params.length);
	req.setRequestHeader("Connection", "close");		
	req.send(params);
}

function validateForm() {
	var req = new XMLHttpRequest();
	req.onreadystatechange=function() {
		if (req.readyState == 4 && req.status == 200) {
		    	if(req.responseText == 1){
		    		document.regForm.submit.disabled = false;
		    	}
		    	else {
		    		document.regForm.submit.disabled = true;
		    	}
		}
	}
	var params = "username=" + document.getElementById('username').value + "&password=" + document.getElementById('password').value 
		+ "&email=" + document.getElementById('email').value
		+ "&usernameCheck=" + document.getElementById('usernameCheck').innerHTML 
		+ "&validatePass=" + document.getElementById('validatePass').innerHTML
		+ "&validateEmail=" + document.getElementById('validateEmail').innerHTML
		+ "&emailCheck=" + document.getElementById('checkExEmail').innerHTML;
	req.open("POST","validateFormAjax.php", true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-length", params.length);
	req.setRequestHeader("Connection", "close");		
	req.send(params);
	
	//repeat the function over 50ms to check if the form is correctly filled
	setTimeout("validateForm()", 50);
}


function checkEmail(email) {
	var req = new XMLHttpRequest();
	req.onreadystatechange=function() {
		if (req.readyState == 4 && req.status == 200) {
		    	document.getElementById("checkExEmail").innerHTML = req.responseText;
		}
	}
	var params = "email=" + email;
	req.open("POST","checkEmailAjax.php", true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-length", params.length);
	req.setRequestHeader("Connection", "close");		
	req.send(params);
	return pass;
}